<?php declare(strict_types=1);

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Http\Requests\UserRequest;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function register(UserRequest $request)
    {
        $username = $request->input("username");
        $password = $request->input("password");

        if (empty($username) || empty($password)) {
            throw new ApiException("用户名或密码不能为空",425);
        }

        $passMixSalt = $this->passwordService->mixSalt($password);
        $pass = $passMixSalt["pass"];
        $salt = $passMixSalt["salt"];

        $result = $this->userService->register($username, $pass, $salt);
        return jsonResponse($result);
    }
}
